﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using AbstractFramework.Statistic;
using AbstractFramework.LogicControl;
using System.IO;

namespace WpfElevator.ControlPart
{
    public class Statistic : IStatistic
    {
                //用一个静态变量来记录Statistic唯一的一个实例
        private static Statistic uniqueStatistic;

        //把构造器声明为私有的，只有自己才能调用构造器
        //private Statistic(){}
        public int ELEVATORNUM;
        private Statistic(int ELEVATORNUM) //需要电梯的个数作为参数
        {
            this.ELEVATORNUM = ELEVATORNUM;
            //各个计时器开始计时
            time = new Timer[ELEVATORNUM];
            globalTimer = new Timer();
            globalTimer.creatTimer();
            for (int i = 0; i < ELEVATORNUM; i++)
            {
                time[i] = new Timer();
                time[i].creatTimer();
            }
            //初始化各个数组
            idleTime = new long[ELEVATORNUM];
            PeopleSum = new int[ELEVATORNUM];
            currentPeople = new int[ELEVATORNUM];
            //responseTime = new ArrayList<Timer>[ELEVATORNUM];
        }
        //用getStatistic方法实例化对象
        public static Statistic getStatistic(int ELEVATORNUM)
        {
            if (uniqueStatistic == null)
            {
                uniqueStatistic = new Statistic(ELEVATORNUM);
            }
            return uniqueStatistic;
        }

        private ITimer globalTimer;//用来记录总时间
        public ITimer GlobalTimer
        {
            get { return globalTimer; }
            set { globalTimer = value; }
        }
        private ITimer[] m_Time;//每个电梯对应一个计时器
        public ITimer[] time
        {
            get { return m_Time; }
            set { m_Time = value; }
        }
        private long[] m_IdleTime;//每个电梯对应一个记录空闲时间的idleTime
        public long[] idleTime
        {
            get { return m_IdleTime; }
            set { m_IdleTime = value; }
        }
        private int[] m_PeopleSum;//每个电梯对应一个记录乘梯人数的PeopleNum
        public int[] PeopleSum
        {
            get { return m_PeopleSum; }
            set { m_PeopleSum = value; }
        }
        private int[] m_CurrentPeople;//记录每台电梯中当前人数
        public int[] currentPeople
        {
            get { return m_CurrentPeople; }
            set { m_CurrentPeople = value; }
        }

        public void updateTime(int ID)//更新相应电梯的累计空闲时间，供controller调用
        {
            idleTime[ID] += time[ID].end();
        }

        public void updatePeopleSum(int ID, int increment)//更新相应电梯的累计人数，供界面调用
        {
            PeopleSum[ID] += increment;
        }

        public string saveStatResult() //把最后结果存到文件中去
        {
            StreamWriter sw = new StreamWriter("result.txt");
            for (int i = 0; i < ELEVATORNUM; i++)
                idleTime[i] += time[i].end();
            long worktime = globalTimer.end();
            sw.WriteLine("电梯的总工作时间（单位是秒）：" + worktime / 1000);
            for (int i = 0; i < ELEVATORNUM; i++)
            {
                sw.WriteLine(string.Format("电梯{0}的空闲率为:\t{1}%\t平均每分钟乘客人数为：\t{2}；",
                    i + 1, Math.Round((((double)idleTime[i]) / worktime),4)*100, PeopleSum[i] * 60000 / worktime));
            }
            sw.Close();
            return "result.txt";
        }

        //ArrayList <Timer> [] responseTime;
        //public void beginTime(int ID)
        //{
        //    Timer t = new Timer();
        //    responseTime[ID].add(t);
        //}
    }
}
